package com.hk.player.utils;

import com.hk.player.Watermark;
import com.zhoug.common.content.SharedPreferencesWrap;
import com.zhoug.common.utils.SpUtils;

/**
 * @author: zhoug
 * @since: 2025-08-06
 */
public class PlayerDataManager {
    private Watermark watermark;//视频水印

    private static final class InstanceHolder {
        private static final PlayerDataManager instance = new PlayerDataManager();
    }

    public static PlayerDataManager getInstance(){
        return InstanceHolder.instance;
    }


    public Watermark getWatermark() {
        if (watermark == null) {
            watermark = getWatermarkText();
        }
        return watermark;
    }

    /**
     * 更新水印
     *
     * @param watermark
     */
    public void setWatermark(Watermark watermark) {
        if (this.watermark == null || !this.watermark.equals(watermark)) {
            this.watermark = watermark;
            setWatermarkText(watermark);
        }

    }

    private static SharedPreferencesWrap getPref() {
        return SharedPreferencesWrap.with(SpUtils.getSharedPreferences("hk_player"));
    }


    /**
     * 保存视频水印
     *
     * @param watermark
     */
    private static void setWatermarkText(Watermark watermark) {
        SharedPreferencesWrap sp = getPref();
        SharedPreferencesWrap.Editor edit = sp.edit();
        String loginUserId = PlayerProviderManager.getHkPlayerProvider().getLoginUserId();
        if (loginUserId == null) {
            loginUserId = "cs000000";
        }
        edit.putObject(loginUserId, watermark);
        edit.apply();
    }

    /**
     * 获取视频水印
     *
     * @return
     */
    private static Watermark getWatermarkText() {
        SharedPreferencesWrap sp = getPref();
        String loginUserId = PlayerProviderManager.getHkPlayerProvider().getLoginUserId();
        if (loginUserId == null) {
            loginUserId = "cs000000";
        }
        return sp.getObject(loginUserId, Watermark.class);
    }


}
